﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Zxb.DayWork.Logic;
using System.Xml;
using Zxb.DayWork.Core;

namespace Zxb.OneTimeWork
{
    public class StatDealerTotalWorker : BatchWorker
    {
        private string urlFormat = System.Configuration.ConfigurationManager.AppSettings["Tel400"];
        private string zxTelCode = System.Configuration.ConfigurationManager.AppSettings["Tel400zx"];
        private string jcTelCode = System.Configuration.ConfigurationManager.AppSettings["Tel400jc"];
        //private string urlProductPrice = System.Configuration.ConfigurationManager.AppSettings["ProductPrice"];

        private DateTime enddate;
        private DateTime startdate;
        private ZxConstant zxconstant;

        private DataTable userDtZx;
        private DataTable userDtJc;
        private DataTable caseDtZxJc;
        private DataTable promotionDtZx;
        private DataTable promotionDtJc;
        private DataTable refreshDtZxJc;
        //private DataTable reserveDtZxJc;
        private DataTable reserveDtZx;
        //private List<PhoneNum> zxTelList;
        //private List<ProductPriceNum> zxProductPriceList;
        private DataTable caseHitDtZxJc;
        private DataTable promotionHitDtZx;
        private DataTable pageHitDtZx;
        private DataTable userCaseMapDtZxJc;
        private DataTable userPromotionMapDtZx;
        private DataTable productDt;
        private DataTable priceDt;
        private DataTable dayHitNumJcDt;

        IEnumerable<StatDealerModel> zxUserList;
        IEnumerable<StatDealerModel> jcUserList;
        IEnumerable<StatDealerModel> zxDayCaseNum;
        IEnumerable<StatDealerModel> jcDayCaseNum;
        IEnumerable<StatDealerModel> zxDayProNum;
        IEnumerable<StatDealerModel> jcDayProNum;
        IEnumerable<StatDealerModel> zxDayReserveNum;
        //IEnumerable<StatDealerModel> jcDayReserveNum;
        IEnumerable<StatDealerModel> zxDayRefreshNum;
        IEnumerable<StatDealerModel> jcDayRefreshNum;
        IEnumerable<StatDealerModel> zxDayProductNum;
        IEnumerable<StatDealerModel> jcDayProductNum;
        IEnumerable<StatDealerModel> zxDayPriceNum;
        IEnumerable<StatDealerModel> jcDayPriceNum;
        IEnumerable<StatDealerModel> zxDayTelNum;
        IEnumerable<StatDealerModel> jcDayTelNum;
        IEnumerable<StatDealerModel> zxDayHitNumZx;
        IEnumerable<StatDealerModel> zxDayHitNumJc;//装修用户在建材站点的点击量(产品报价页面)
        IEnumerable<StatDealerModel> jcDayHitNumZx;//建材用户在装修站点的点击量(案例页面)
        IEnumerable<StatDealerModel> jcDayHitNumJc;

        DataTable targetDTzx;
        DataTable targetDTjc;

        public StatDealerTotalWorker()
        {
            startdate = TypeHelper.GetDay(DateTime.Today.AddDays(-1.0));
            enddate = startdate.AddDays(1.0);
            zxconstant = ZxConstantList.GetZxConstant(ZxJc.All);
        }
        //public StatDealerWorker(DateTime start, DateTime end, ZxJc z)
        //{
        //    startdate = start;
        //    enddate = end;
        //    zxconstant = ZxConstantList.GetZxConstant(z);
        //}
        public StatDealerTotalWorker(int lastNday)
        {
            if (lastNday > -1)
            {
                lastNday = -1;
            }
            startdate = TypeHelper.GetDay(DateTime.Now.AddDays(lastNday));
            enddate = startdate.AddDays(1.0);
            zxconstant = ZxConstantList.GetZxConstant(ZxJc.All);
        }

        private DataTable ZxUser()
        {
            string sql = "select u.ID,u.SoufunName,d.DealerName,u.CityName from UserDealerExtend d with(nolock) inner join [User] u with(nolock) on d.UserID=u.ID";
            return DAL.ReadDt_Retry_ReturnNewIfNull(DB.TaskReader, sql);
        }

        public DataTable CaseCount()
        {
            string sql = string.Format("select WeiKe_UserID,COUNT(1) as caseNum from Cases with(nolock) where AddedTime>='{0}' and AddedTime<'{1}' and IsValid=1 {2} group by WeiKe_UserID", startdate.ToShortDateString(), enddate.ToShortDateString(), zxconstant.whereCase);
            return DAL.ReadDt_Retry_ReturnNewIfNull(DB.TaskReader, sql);
        }

        public DataTable PromotionCount()
        {
            string sql = string.Format("select DesignerID,count(1) as proNum from CompanyNews with(nolock) where PublishDate>='{0}' and PublishDate<'{1}' and IsDel=0 {2} group by DesignerID"
            , startdate.ToShortDateString(), enddate.ToShortDateString(), zxconstant.wherePro);
            return DAL.ReadDt_Retry_ReturnNewIfNull(DB.TaskReader, sql);
        }

        public DataTable RefreshCount()
        {
            string sql = string.Format("select OperateUserID,COUNT(1) as refreshNum from opLog with(nolock) where RefreshTime>='{0}' and RefreshTime<'{1}' {2} group by OperateUserID"
            , startdate.ToShortDateString(), enddate.ToShortDateString(), zxconstant.whereRefresh);
            return DAL.ReadDt_Retry_ReturnNewIfNull(DB.ZxbReader, sql);
        }

        //public DataTable ReserveCount()
        //{
        //    string sql = string.Format("select channel,UserID,COUNT(1) as reserveNum from RefreshReserve with(nolock) where [time]>='{0}' and [time]<'{1}' {2} group by channel,UserID "
        //    , startdate.ToShortDateString(), enddate.ToShortDateString(), zxconstant.whereReserve);
        //    return DAL.ReadDt_ReturnNewIfNull(DB.ZxbReader, sql);
        //}
        public DataTable zxReserveCount()
        {
            string sql = string.Format("select DealerID,COUNT(1) as reserveNum from DealerReservation with(nolock) where CreateTime>='{0}' and CreateTime<'{1}' group by DealerID"
            , startdate.ToShortDateString(), enddate.ToShortDateString());
            return DAL.ReadDt_Retry_ReturnNewIfNull(DB.TaskReader, sql);
        }

        public DataTable CaseHit()
        {
            string sql = string.Format("select ObjectID,sum(RandCount) as Hits_Count from [HitsWeiKe_Real] with(nolock)  where VisitTime>='{0}' and VisitTime <'{1}' and TypeID=5 group by ObjectID"
            , startdate.ToShortDateString(), enddate.ToShortDateString());
            return DAL.ReadDt_Retry_ReturnNewIfNull(DB.Home_logAdmin, sql);
        }
        public DataTable PromotionHit()
        {
            string sql = string.Format("select ObjectID,sum(RandCount) as Hits_Count from [HitsWeiKe_Real] with(nolock)  where VisitTime>='{0}' and VisitTime <'{1}' and TypeID=6 group by ObjectID"
            , startdate.ToShortDateString(), enddate.ToShortDateString());
            return DAL.ReadDt_Retry_ReturnNewIfNull(DB.Home_logAdmin, sql);
        }
        public DataTable PageHit()
        {
            string sql = string.Format("select ObjectID,sum(RandCount) as Hits_Count from [HitsWeiKe_Real] with(nolock)  where VisitTime>='{0}' and VisitTime <'{1}' and TypeID=7 group by ObjectID"
            , startdate.ToShortDateString(), enddate.ToShortDateString());
            return DAL.ReadDt_Retry_ReturnNewIfNull(DB.Home_logAdmin, sql);
        }
        public DataTable UserCaseMap()
        {
            string sql = "select CaseID,WeiKe_UserID from Cases with(nolock)";
            return DAL.ReadDt_Retry_ReturnNewIfNull(DB.TaskReader, sql);
        }
        public DataTable UserPromotionMap()
        {
            string sql = "select ID,DesignerID from CompanyNews with(nolock)";
            return DAL.ReadDt_Retry_ReturnNewIfNull(DB.TaskReader, sql);
        }

        //public List<PhoneNum> TelCountZx()
        //{
        //    List<PhoneNum> list = new List<PhoneNum>();
        //    string url = string.Format(urlFormat, zxTelCode, startdate.ToString("yyyy-MM-dd"));
        //    string txt = Net.GetStringFromUrl_Retry(url);
        //    XmlDocument xmldoc = new XmlDocument();
        //    xmldoc.LoadXml(txt);

        //    XmlNodeList nodes = xmldoc.SelectNodes("result/homesoufun_400/common");
        //    foreach (XmlNode node in nodes)
        //    {
        //        int userid = TypeHelper.ParseInt(node.SelectSingleNode("ObjectID").InnerText);
        //        int num = TypeHelper.ParseInt(node.SelectSingleNode("PhoneNum").InnerText);
        //        list.Add(new PhoneNum() { UserID = userid, TelNum = num });
        //    }
        //    var query =
        //        from u in list
        //        group u by u.UserID into g
        //        select new PhoneNum
        //        {
        //            UserID = g.Key,
        //            TelNum = g.Sum(x => x.TelNum)
        //        };
        //    list = query.ToList();
        //    //foreach (DataRow row in zxUserDt.Rows)
        //    //{
        //    //    //list.Add(
        //    //    int userid = TypeHelper.ParseInt(row["ID"]);
        //    //    string url = string.Format(urlFormat, zxTelCode, userid, startdate.ToString("yyyy-MM-dd"));
        //    //    string txt = Net.GetStringFromUrl_Retry(url);
        //    //    XmlDocument xmldoc = new XmlDocument();
        //    //    xmldoc.LoadXml(txt);

        //    //    XmlNodeList nodes = xmldoc.SelectNodes("result/homesoufun_400/common");
        //    //    foreach (XmlNode node in nodes)
        //    //    {
        //    //        int num = TypeHelper.ParseInt(node.SelectSingleNode("PhoneNum").InnerText);
        //    //        list.Add(new PhoneNum() { UserID = userid, TelNum = num });
        //    //    }
        //    //    if (xmldoc.SelectSingleNode("result/homesoufun_400/common/PhoneNum") != null)
        //    //    {
        //    //        XmlNode node = xmldoc.SelectSingleNode("result/homesoufun_400/common/PhoneNum");
        //    //        int num = TypeHelper.ParseInt(node.InnerText);
        //    //        list.Add(new PhoneNum() { UserID = userid, TelNum = num });
        //    //    }
        //    //}
        //    return list;
        //}

        public IEnumerable<StatDealerModel> TelCount(string telCode)
        {
            List<StatDealerModel> list = new List<StatDealerModel>();
            string url = string.Format(urlFormat, telCode, startdate.ToString("yyyy-MM-dd"));
            string txt = Net.GetStringFromUrl_Retry(url);
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(txt);

            XmlNodeList nodes = xmldoc.SelectNodes("result/homesoufun_400/common");
            foreach (XmlNode node in nodes)
            {
                int userid = TypeHelper.ParseInt(node.SelectSingleNode("ObjectID").InnerText);
                int num = TypeHelper.ParseInt(node.SelectSingleNode("PhoneNum").InnerText);
                list.Add(new StatDealerModel() { DealerID = userid, DayTelNum = num });
            }
            var query =
                from u in list
                group u by u.DealerID into g
                select new StatDealerModel
                {
                    DealerID = g.Key,
                    DayTelNum = g.Sum(x => x.DayTelNum)
                };
            return query;
        }

        //public List<ProductPriceNum> ProductPriceZx()
        //{
        //    List<ProductPriceNum> list = new List<ProductPriceNum>();

        //    //foreach (DataRow row in zxUserDt.Rows)
        //    //{
        //    //    int userid = TypeHelper.ParseInt(row["ID"]);
        //    //    string url = string.Format(urlProductPrice, startdate.ToString("yyyy-MM-dd"), userid * (-1));
        //    //    string txt = Net.GetStringFromUrl_Retry(url);
        //    //    if (txt == null)
        //    //    {
        //    //        continue;
        //    //    }
        //    //    XmlDocument xmldoc = new XmlDocument();
        //    //    xmldoc.LoadXml(txt);

        //    //    XmlNode node = xmldoc.SelectSingleNode("DealerInfo/ProductCount");
        //    //    int productnum = TypeHelper.ParseInt(node.InnerText);
        //    //    node = xmldoc.SelectSingleNode("DealerInfo/CatalogProductCount");
        //    //    int pricenum = TypeHelper.ParseInt(node.InnerText);
        //    //    node = xmldoc.SelectSingleNode("DealerInfo/Hit");
        //    //    int hitnum = TypeHelper.ParseInt(node.InnerText);
        //    //    if (productnum > 0 || productnum > 0 || hitnum > 0)
        //    //    {
        //    //        list.Add(new ProductPriceNum() { UserID = userid, ProductNum = productnum, PriceNum = pricenum, JcHit = hitnum });
        //    //    }
        //    //}
        //    return list;
        //}

        private void GetCaseNumZxJc()
        {
            var query =
                from c in caseDtZxJc.AsEnumerable()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = TypeHelper.ParseInt(c.Field<object>("WeiKe_UserID")) > 0 ? 2 : 1,
                    DealerID = Math.Abs(TypeHelper.ParseInt(c.Field<object>("WeiKe_UserID"))),
                    DayCaseNum = TypeHelper.ParseInt(c.Field<object>("caseNum"))
                };
            zxDayCaseNum =
                from c in query
                where c.DealerType == 2
                select c;
            jcDayCaseNum =
                from c in query
                where c.DealerType == 1
                select c;
        }
        private void GetProNumZx()
        {
            zxDayProNum =
                from p in promotionDtZx.AsEnumerable()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 2,
                    DealerID = TypeHelper.ParseInt(p.Field<object>("DesignerID")),
                    DayProNum = TypeHelper.ParseInt(p.Field<object>("proNum"))
                };
        }
        private void GetProNumJc()
        {
            jcDayProNum =
                from p in promotionDtJc.AsEnumerable()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 1,
                    DealerID = TypeHelper.ParseInt(p.Field<object>("dealerId")),
                    DayProNum = TypeHelper.ParseInt(p.Field<object>("counts"))
                };
        }
        private void GetReserveNumZxJc()
        {
            //var query=
            //    from c in reserveDtZxJc.AsEnumerable()
            //    select new StatDealerModel
            //    {
            //        StatDate = startdate,
            //        DealerType = TypeHelper.ParseInt(c.Field<object>("channel")),
            //        DealerID = TypeHelper.ParseInt(c.Field<object>("UserID")),
            //        DayReserveNum = TypeHelper.ParseInt(c.Field<object>("reserveNum"))
            //    };
            //zxDayReserveNum =
            //    from c in query
            //    where c.DealerType == 2
            //    select c;
            //jcDayReserveNum =
            //    from c in query
            //    where c.DealerType == 1
            //    select c;
            zxDayReserveNum =
                from c in reserveDtZx.AsEnumerable()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 2,
                    DealerID = TypeHelper.ParseInt(c.Field<object>("DealerID")),
                    DayReserveNum = TypeHelper.ParseInt(c.Field<object>("reserveNum"))
                };
        }
        private void GetRefreshNumZxJc()
        {
            var query =
                from c in refreshDtZxJc.AsEnumerable()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = TypeHelper.ParseInt(c.Field<object>("OperateUserID")) > 400000 ? 1 : 2,
                    DealerID = TypeHelper.ParseInt(c.Field<object>("OperateUserID")),
                    DayRefreshNum = TypeHelper.ParseInt(c.Field<object>("refreshNum"))
                };
            zxDayRefreshNum =
                from c in query
                where c.DealerType == 2
                select c;
            //jcDayRefreshNum =
            //    from c in query
            //    where c.DealerType == 1
            //    select c;
            var query2 =
                from c in query
                where c.DealerType == 1
                select c;
            jcDayRefreshNum =
                from c in query2
                join u in userDtJc.AsEnumerable()
                on c.DealerID equals u.Field<object>("UserID")
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 1,
                    DealerID = TypeHelper.ParseInt(u.Field<object>("DealerID")),
                    DayRefreshNum = c.DayRefreshNum
                };
        }
        private void GetProductNumZxJc()
        {
            var query =
                from c in productDt.AsEnumerable()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = TypeHelper.ParseInt(c.Field<object>("dealerId")) >= 0 ? 1 : 2,
                    DealerID = Math.Abs(TypeHelper.ParseInt(c.Field<object>("dealerId"))),
                    DayProductNum = TypeHelper.ParseInt(c.Field<object>("counts"))
                };
            zxDayProductNum =
                from c in query
                where c.DealerType == 2
                select c;
            jcDayProductNum =
                from c in query
                where c.DealerType == 1
                select c;
        }
        private void GetPriceNumZxJc()
        {
            var query =
                from c in priceDt.AsEnumerable()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = TypeHelper.ParseInt(c.Field<object>("dealerId")) >= 0 ? 1 : 2,
                    DealerID = Math.Abs(TypeHelper.ParseInt(c.Field<object>("dealerId"))),
                    DayPriceNum = TypeHelper.ParseInt(c.Field<object>("counts"))
                };
            zxDayPriceNum =
                from c in query
                where c.DealerType == 2
                select c;
            jcDayPriceNum =
                from c in query
                where c.DealerType == 1
                select c;
        }
        private void GetTelNumZxJc()
        {
            zxDayTelNum = TelCount(zxTelCode);
            jcDayTelNum = TelCount(jcTelCode);
        }
        private void GetUserListZxJc()
        {
            zxUserList =
                from c in userDtZx.AsEnumerable()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 2,
                    DealerID = TypeHelper.ParseInt(c.Field<object>("ID")),
                    CityName = c.Field<object>("CityName").ToString(),
                    DealerName = c.Field<object>("DealerName").ToString(),
                    UserName = c.Field<object>("SoufunName").ToString()
                };
            jcUserList =
                from c in userDtJc.AsEnumerable()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 1,
                    DealerID = TypeHelper.ParseInt(c.Field<object>("DealerID")),
                    CityName = c.Field<object>("CityName").ToString(),
                    DealerName = c.Field<object>("DealerName").ToString(),
                    UserName = c.Field<object>("UserName").ToString()
                };
        }
        private void GetZxJcDayHitNumZx()
        {
            var zxjc_UserCaseHit_Pre =
                from u in userCaseMapDtZxJc.AsEnumerable()
                join c in caseHitDtZxJc.AsEnumerable()
                on TypeHelper.ParseInt(u.Field<object>("CaseID")) equals TypeHelper.ParseInt(c.Field<object>("ObjectID")) into uc
                from item in uc.DefaultIfEmpty()
                select new
                {
                    UserID = TypeHelper.ParseInt(u.Field<object>("WeiKe_UserID")),//负的是建材
                    CaseID = TypeHelper.ParseInt(u.Field<object>("CaseID")),
                    CaseHit = (item == null) ? 0 : TypeHelper.ParseInt(item.Field<object>("Hits_Count"))
                };
            var zxjc_UserCaseHit =
                from u in zxjc_UserCaseHit_Pre
                group u by u.UserID into g
                select new
                {
                    UserID = g.Key,
                    CaseHit = g.Sum(x => x.CaseHit)
                };
            jcDayHitNumZx =
                from u in zxjc_UserCaseHit
                where u.UserID < 0
                select new StatDealerModel
                {
                    DealerID = u.UserID * (-1),
                    DayHitNumZx = u.CaseHit
                };

            var zx_UserProHitPre =
                from u in userPromotionMapDtZx.AsEnumerable()
                join c in promotionHitDtZx.AsEnumerable()
                on TypeHelper.ParseInt(u.Field<object>("ID")) equals TypeHelper.ParseInt(c.Field<object>("ObjectID")) into uc
                from item in uc.DefaultIfEmpty()
                select new
                {
                    UserID = TypeHelper.ParseInt(u.Field<object>("DesignerID")),
                    ProID = TypeHelper.ParseInt(u.Field<object>("ID")),
                    ProHit = (item == null) ? 0 : TypeHelper.ParseInt(item.Field<object>("Hits_Count"))
                };
            var zx_UserProHit =
                from u in zx_UserProHitPre
                group u by u.UserID into g
                select new
                {
                    UserID = g.Key,
                    ProHit = g.Sum(x => x.ProHit)
                };

            var zx_hit_User_Case =
                from u in userDtZx.AsEnumerable()
                join c in zxjc_UserCaseHit
                on TypeHelper.ParseInt(u.Field<object>("ID")) equals c.UserID into uc
                from item in uc.DefaultIfEmpty()
                select new
                {
                    UserID = TypeHelper.ParseInt(u.Field<object>("ID")),
                    Hit = (item == null) ? 0 : item.CaseHit
                };
            var zx_hit_User_Case_Pro =
                from u in zx_hit_User_Case
                join c in zx_UserProHit
                on u.UserID equals c.UserID into uc
                from item in uc.DefaultIfEmpty()
                select new
                {
                    UserID = u.UserID,
                    Hit = (item == null) ? u.Hit : u.Hit + item.ProHit
                };
            var zx_hit_User_Case_Pro_Page =
                from u in zx_hit_User_Case_Pro
                join c in pageHitDtZx.AsEnumerable()
                on u.UserID equals TypeHelper.ParseInt(c.Field<object>("ObjectID")) into uc
                from item in uc.DefaultIfEmpty()
                select new StatDealerModel
                {
                    DealerType = 2,
                    DealerID = u.UserID,
                    DayHitNumZx = (item == null) ? u.Hit : u.Hit + TypeHelper.ParseInt(item.Field<object>("Hits_Count"))
                };
            zxDayHitNumZx = zx_hit_User_Case_Pro_Page;
        }
        private void GetZxJcDayHitNumJc()
        {
            Random rm = new Random(DateTime.Now.Millisecond);
            var zxjcDayHitNumJc =
                from u in dayHitNumJcDt.AsEnumerable()
                select new StatDealerModel
                {
                    DealerType = TypeHelper.ParseInt(u.Field<object>("dealerId")) >= 0 ? 1 : 2,
                    DealerID = Math.Abs(TypeHelper.ParseInt(u.Field<object>("dealerId"))),
                    DayHitNumJc = TypeHelper.ParseInt(u.Field<object>("counts"))
                };
            zxDayHitNumJc =
                from u in zxjcDayHitNumJc
                where u.DealerType == 2
                select u;
            jcDayHitNumJc =
                from u in zxjcDayHitNumJc
                where u.DealerType == 1
                select u;
        }

        //public void Run()
        //{
        //    caseDt = CaseCount();
        //    promotionDt = PromotionCount();
        //    refreshDt = RefreshCount();
        //    reserveDt = ReserveCount();
        //}

        protected override void ReadData()
        {
            caseDtZxJc = CaseCount();
            promotionDtZx = PromotionCount();
            refreshDtZxJc = RefreshCount();
            //reserveDtZxJc = ReserveCount();
            reserveDtZx = zxReserveCount();
            userDtZx = ZxUser();
            //zxTelList = TelCountZx();
            //zxProductPriceList = ProductPriceZx();
            userDtJc = StatDealerWorkerJC.getJCDealer();
            productDt = StatDealerWorkerJC.getProductbyDate(startdate);
            priceDt = StatDealerWorkerJC.getCatalogProducts(startdate);
            promotionDtJc = StatDealerWorkerJC.getPromotionCount(startdate);
            dayHitNumJcDt = StatDealerWorkerJC.getHitsByDate(startdate);

            caseHitDtZxJc = CaseHit();
            promotionHitDtZx = PromotionHit();
            pageHitDtZx = PageHit();
            userCaseMapDtZxJc = UserCaseMap();
            userPromotionMapDtZx = UserPromotionMap();

            //targetDTjc = StatDealerWorkerJC.processJCDealer(startdate);
            GetUserListZxJc();
            GetCaseNumZxJc();
            GetProNumZx();
            GetProNumJc();
            GetReserveNumZxJc();
            GetRefreshNumZxJc();
            GetProductNumZxJc();
            GetPriceNumZxJc();
            GetTelNumZxJc();
            GetZxJcDayHitNumZx();
            GetZxJcDayHitNumJc();
        }

        private void ProcessDealerZx()
        {
            var zx_User_Case =
                from u in zxUserList
                join c in zxDayCaseNum
                on u.DealerID equals c.DealerID into uc
                from item in uc.DefaultIfEmpty()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 2,
                    DealerID = u.DealerID,
                    UserName = u.UserName,
                    CityName = u.CityName,
                    DealerName = u.DealerName,
                    DayCaseNum = (item == null) ? 0 : item.DayCaseNum

                };
            var zx_User_Case_Pro =
                from u in zx_User_Case
                join c in zxDayProNum
                on u.DealerID equals c.DealerID into uc
                from item in uc.DefaultIfEmpty()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 2,
                    DealerID = u.DealerID,
                    UserName = u.UserName,
                    CityName = u.CityName,
                    DealerName = u.DealerName,
                    DayCaseNum = u.DayCaseNum,
                    DayProNum = (item == null) ? 0 : item.DayProNum
                };
            var zx_User_Case_Pro_Res =
                from u in zx_User_Case_Pro
                join c in zxDayReserveNum
                on u.DealerID equals c.DealerID into uc
                from item in uc.DefaultIfEmpty()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 2,
                    DealerID = u.DealerID,
                    UserName = u.UserName,
                    CityName = u.CityName,
                    DealerName = u.DealerName,
                    DayCaseNum = u.DayCaseNum,
                    DayProNum = u.DayProNum,
                    DayReserveNum = (item == null) ? 0 : item.DayReserveNum
                };
            var zx_User_Case_Pro_Res_Ref =
                from u in zx_User_Case_Pro_Res
                join c in zxDayRefreshNum
                on u.DealerID equals c.DealerID into uc
                from item in uc.DefaultIfEmpty()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 2,
                    DealerID = u.DealerID,
                    UserName = u.UserName,
                    CityName = u.CityName,
                    DealerName = u.DealerName,
                    DayCaseNum = u.DayCaseNum,
                    DayProNum = u.DayProNum,
                    DayReserveNum = u.DayReserveNum,
                    DayRefreshNum = (item == null) ? 0 : item.DayRefreshNum
                };
            var zx_User_Case_Pro_Res_Ref_Pdt =
                from u in zx_User_Case_Pro_Res_Ref
                join c in zxDayProductNum
                on u.DealerID equals c.DealerID into uc
                from item in uc.DefaultIfEmpty()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 2,
                    DealerID = u.DealerID,
                    UserName = u.UserName,
                    CityName = u.CityName,
                    DealerName = u.DealerName,
                    DayCaseNum = u.DayCaseNum,
                    DayProNum = u.DayProNum,
                    DayReserveNum = u.DayReserveNum,
                    DayRefreshNum = u.DayRefreshNum,
                    DayProductNum = (item == null) ? 0 : item.DayProductNum
                };
            var zx_User_Case_Pro_Res_Ref_Pdt_Price =
                from u in zx_User_Case_Pro_Res_Ref_Pdt
                join c in zxDayPriceNum
                on u.DealerID equals c.DealerID into uc
                from item in uc.DefaultIfEmpty()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 2,
                    DealerID = u.DealerID,
                    UserName = u.UserName,
                    CityName = u.CityName,
                    DealerName = u.DealerName,
                    DayCaseNum = u.DayCaseNum,
                    DayProNum = u.DayProNum,
                    DayReserveNum = u.DayReserveNum,
                    DayRefreshNum = u.DayRefreshNum,
                    DayProductNum = u.DayProductNum,
                    DayPriceNum = (item == null) ? 0 : item.DayPriceNum
                };
            var zx_User_Case_Pro_Res_Ref_Pdt_Price_HitZx =
                from u in zx_User_Case_Pro_Res_Ref_Pdt_Price
                join c in zxDayHitNumZx
                on u.DealerID equals c.DealerID into uc
                from item in uc.DefaultIfEmpty()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 2,
                    DealerID = u.DealerID,
                    UserName = u.UserName,
                    CityName = u.CityName,
                    DealerName = u.DealerName,
                    DayCaseNum = u.DayCaseNum,
                    DayProNum = u.DayProNum,
                    DayReserveNum = u.DayReserveNum,
                    DayRefreshNum = u.DayRefreshNum,
                    DayProductNum = u.DayProductNum,
                    DayPriceNum = u.DayPriceNum,
                    DayHitNumZx = (item == null) ? 0 : item.DayHitNumZx
                };
            var zx_User_Case_Pro_Res_Ref_Pdt_Price_HitZx_HitJc =
                from u in zx_User_Case_Pro_Res_Ref_Pdt_Price_HitZx
                join c in zxDayHitNumJc
                on u.DealerID equals c.DealerID into uc
                from item in uc.DefaultIfEmpty()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 2,
                    DealerID = u.DealerID,
                    UserName = u.UserName,
                    CityName = u.CityName,
                    DealerName = u.DealerName,
                    DayCaseNum = u.DayCaseNum,
                    DayProNum = u.DayProNum,
                    DayReserveNum = u.DayReserveNum,
                    DayRefreshNum = u.DayRefreshNum,
                    DayProductNum = u.DayProductNum,
                    DayPriceNum = u.DayPriceNum,
                    DayHitNumZx = u.DayHitNumZx,
                    DayHitNumJc = (item == null) ? 0 : item.DayHitNumJc
                };
            var zx_User_Case_Pro_Res_Ref_Pdt_Price_HitZx_HitJc_Tel =
                from u in zx_User_Case_Pro_Res_Ref_Pdt_Price_HitZx_HitJc
                join c in zxDayTelNum
                on u.DealerID equals c.DealerID into uc
                from item in uc.DefaultIfEmpty()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 2,
                    DealerID = u.DealerID,
                    UserName = u.UserName,
                    CityName = u.CityName,
                    DealerName = u.DealerName,
                    DayCaseNum = u.DayCaseNum,
                    DayProNum = u.DayProNum,
                    DayReserveNum = u.DayReserveNum,
                    DayRefreshNum = u.DayRefreshNum,
                    DayProductNum = u.DayProductNum,
                    DayPriceNum = u.DayPriceNum,
                    DayHitNumZx = u.DayHitNumZx,
                    DayHitNumJc = u.DayHitNumJc,
                    DayHitNum = u.DayHitNumJc + u.DayHitNumZx,
                    DayTelNum = (item == null) ? 0 : item.DayTelNum

                };
            var result =
                from z in zx_User_Case_Pro_Res_Ref_Pdt_Price_HitZx_HitJc_Tel
                where (z.DayCaseNum > 0 || z.DayProNum > 0 || z.DayRefreshNum > 0 || z.DayReserveNum > 0 || z.DayTelNum > 0
                || z.DayProductNum > 0 || z.DayPriceNum > 0 || z.DayHitNum > 0) && z.DealerID != 0
                select z;

            targetDTzx = PrepareSchemaUdtStatDealer();

            foreach (var item in result)
            {
                DataRow row = targetDTzx.NewRow();
                row["StatDate"] = TypeHelper.GetDay(item.StatDate);
                row["DealerType"] = item.DealerType;
                row["DealerID"] = item.DealerID;
                row["DayCaseNum"] = item.DayCaseNum;
                row["DayProNum"] = item.DayProNum;
                row["DayProductNum"] = item.DayProductNum;
                row["DayPriceNum"] = item.DayPriceNum;
                row["DayRefreshNum"] = item.DayRefreshNum;
                row["DayReserveNum"] = item.DayReserveNum;
                row["DayHitNum"] = item.DayHitNum;
                row["DayTelNum"] = item.DayTelNum;

                row["CityName"] = item.CityName;
                row["DealerName"] = item.DealerName;
                row["UserName"] = item.UserName;
                row["DayHitNumZx"] = item.DayHitNumZx;
                row["DayHitNumJc"] = item.DayHitNumJc;

                targetDTzx.Rows.Add(row);
            }
        }
        private void ProcessDealerJc()
        {
            var jc_User_Case =
                from u in jcUserList
                join c in jcDayCaseNum
                on u.DealerID equals c.DealerID into uc
                from item in uc.DefaultIfEmpty()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 1,
                    DealerID = u.DealerID,
                    UserName = u.UserName,
                    CityName = u.CityName,
                    DealerName = u.DealerName,
                    DayCaseNum = (item == null) ? 0 : item.DayCaseNum

                };
            var jc_User_Case_Pro =
                from u in jc_User_Case
                join c in jcDayProNum
                on u.DealerID equals c.DealerID into uc
                from item in uc.DefaultIfEmpty()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 1,
                    DealerID = u.DealerID,
                    UserName = u.UserName,
                    CityName = u.CityName,
                    DealerName = u.DealerName,
                    DayCaseNum = u.DayCaseNum,
                    DayProNum = (item == null) ? 0 : item.DayProNum
                };
            //var jc_User_Case_Pro_Res =
            //    from u in jc_User_Case_Pro
            //    join c in jcDayReserveNum
            //    on u.DealerID equals c.DealerID into uc
            //    from item in uc.DefaultIfEmpty()
            //    select new StatDealerModel
            //    {
            //        StatDate = startdate,
            //        DealerType = 1,
            //        DealerID = u.DealerID,
            //        UserName = u.UserName,
            //        CityName = u.CityName,
            //        DealerName = u.DealerName,
            //        DayCaseNum = u.DayCaseNum,
            //        DayProNum = u.DayProNum,
            //        DayReserveNum = (item == null) ? 0 : item.DayReserveNum
            //    };
            var jc_User_Case_Pro_Res =
                from u in jc_User_Case_Pro
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 1,
                    DealerID = u.DealerID,
                    UserName = u.UserName,
                    CityName = u.CityName,
                    DealerName = u.DealerName,
                    DayCaseNum = u.DayCaseNum,
                    DayProNum = u.DayProNum,
                    DayReserveNum = 0
                };
            var jc_User_Case_Pro_Res_Ref =
                from u in jc_User_Case_Pro_Res
                join c in jcDayRefreshNum
                on u.DealerID equals c.DealerID into uc
                from item in uc.DefaultIfEmpty()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 1,
                    DealerID = u.DealerID,
                    UserName = u.UserName,
                    CityName = u.CityName,
                    DealerName = u.DealerName,
                    DayCaseNum = u.DayCaseNum,
                    DayProNum = u.DayProNum,
                    DayReserveNum = u.DayReserveNum,
                    DayRefreshNum = (item == null) ? 0 : item.DayRefreshNum
                };
            var jc_User_Case_Pro_Res_Ref_Pdt =
                from u in jc_User_Case_Pro_Res_Ref
                join c in jcDayProductNum
                on u.DealerID equals c.DealerID into uc
                from item in uc.DefaultIfEmpty()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 1,
                    DealerID = u.DealerID,
                    UserName = u.UserName,
                    CityName = u.CityName,
                    DealerName = u.DealerName,
                    DayCaseNum = u.DayCaseNum,
                    DayProNum = u.DayProNum,
                    DayReserveNum = u.DayReserveNum,
                    DayRefreshNum = u.DayRefreshNum,
                    DayProductNum = (item == null) ? 0 : item.DayProductNum
                };
            var jc_User_Case_Pro_Res_Ref_Pdt_Price =
                from u in jc_User_Case_Pro_Res_Ref_Pdt
                join c in jcDayPriceNum
                on u.DealerID equals c.DealerID into uc
                from item in uc.DefaultIfEmpty()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 1,
                    DealerID = u.DealerID,
                    UserName = u.UserName,
                    CityName = u.CityName,
                    DealerName = u.DealerName,
                    DayCaseNum = u.DayCaseNum,
                    DayProNum = u.DayProNum,
                    DayReserveNum = u.DayReserveNum,
                    DayRefreshNum = u.DayRefreshNum,
                    DayProductNum = u.DayProductNum,
                    DayPriceNum = (item == null) ? 0 : item.DayPriceNum
                };
            var jc_User_Case_Pro_Res_Ref_Pdt_Price_HitZx =
                from u in jc_User_Case_Pro_Res_Ref_Pdt_Price
                join c in jcDayHitNumZx
                on u.DealerID equals c.DealerID into uc
                from item in uc.DefaultIfEmpty()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 1,
                    DealerID = u.DealerID,
                    UserName = u.UserName,
                    CityName = u.CityName,
                    DealerName = u.DealerName,
                    DayCaseNum = u.DayCaseNum,
                    DayProNum = u.DayProNum,
                    DayReserveNum = u.DayReserveNum,
                    DayRefreshNum = u.DayRefreshNum,
                    DayProductNum = u.DayProductNum,
                    DayPriceNum = u.DayPriceNum,
                    DayHitNumZx = (item == null) ? 0 : item.DayHitNumZx
                };
            var jc_User_Case_Pro_Res_Ref_Pdt_Price_HitZx_HitJc =
                from u in jc_User_Case_Pro_Res_Ref_Pdt_Price_HitZx
                join c in jcDayHitNumJc
                on u.DealerID equals c.DealerID into uc
                from item in uc.DefaultIfEmpty()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 1,
                    DealerID = u.DealerID,
                    UserName = u.UserName,
                    CityName = u.CityName,
                    DealerName = u.DealerName,
                    DayCaseNum = u.DayCaseNum,
                    DayProNum = u.DayProNum,
                    DayReserveNum = u.DayReserveNum,
                    DayRefreshNum = u.DayRefreshNum,
                    DayProductNum = u.DayProductNum,
                    DayPriceNum = u.DayPriceNum,
                    DayHitNumZx = u.DayHitNumZx,
                    DayHitNumJc = (item == null) ? 0 : item.DayHitNumJc
                };
            var jc_User_Case_Pro_Res_Ref_Pdt_Price_HitZx_HitJc_Tel =
                from u in jc_User_Case_Pro_Res_Ref_Pdt_Price_HitZx_HitJc
                join c in jcDayTelNum
                on u.DealerID equals c.DealerID into uc
                from item in uc.DefaultIfEmpty()
                select new StatDealerModel
                {
                    StatDate = startdate,
                    DealerType = 1,
                    DealerID = u.DealerID,
                    UserName = u.UserName,
                    CityName = u.CityName,
                    DealerName = u.DealerName,
                    DayCaseNum = u.DayCaseNum,
                    DayProNum = u.DayProNum,
                    DayReserveNum = u.DayReserveNum,
                    DayRefreshNum = u.DayRefreshNum,
                    DayProductNum = u.DayProductNum,
                    DayPriceNum = u.DayPriceNum,
                    DayHitNumZx = u.DayHitNumZx,
                    DayHitNumJc = u.DayHitNumJc,
                    DayHitNum = u.DayHitNumJc + u.DayHitNumZx,
                    DayTelNum = (item == null) ? 0 : item.DayTelNum

                };
            var result =
                from z in jc_User_Case_Pro_Res_Ref_Pdt_Price_HitZx_HitJc_Tel
                where (z.DayCaseNum > 0 || z.DayProNum > 0 || z.DayRefreshNum > 0 || z.DayReserveNum > 0 || z.DayTelNum > 0
                || z.DayProductNum > 0 || z.DayPriceNum > 0 || z.DayHitNum > 0) && z.DealerID != 0
                select z;

            targetDTjc = PrepareSchemaUdtStatDealer();

            foreach (var item in result)
            {
                DataRow row = targetDTjc.NewRow();
                row["StatDate"] = TypeHelper.GetDay(item.StatDate);
                row["DealerType"] = item.DealerType;
                row["DealerID"] = item.DealerID;
                row["DayCaseNum"] = item.DayCaseNum;
                row["DayProNum"] = item.DayProNum;
                row["DayProductNum"] = item.DayProductNum;
                row["DayPriceNum"] = item.DayPriceNum;
                row["DayRefreshNum"] = item.DayRefreshNum;
                row["DayReserveNum"] = item.DayReserveNum;
                row["DayHitNum"] = item.DayHitNum;
                row["DayTelNum"] = item.DayTelNum;

                row["CityName"] = item.CityName;
                row["DealerName"] = item.DealerName;
                row["UserName"] = item.UserName;
                row["DayHitNumZx"] = item.DayHitNumZx;
                row["DayHitNumJc"] = item.DayHitNumJc;

                targetDTjc.Rows.Add(row);
            }
        }

        protected override void ProcessData()
        {
            ProcessDealerZx();
            ProcessDealerJc();
        }

        //protected override void ProcessData()
        //{
        //    var zx_UserCaseHitPre =
        //        from u in userCaseMapDtZxJc.AsEnumerable()
        //        join c in caseHitDtZxJc.AsEnumerable()
        //        on TypeHelper.ParseInt(u.Field<object>("CaseID")) equals TypeHelper.ParseInt(c.Field<object>("ObjectID")) into uc
        //        from item in uc.DefaultIfEmpty()
        //        select new
        //        {
        //            UserID = TypeHelper.ParseInt(u.Field<object>("WeiKe_UserID")),
        //            CaseID = TypeHelper.ParseInt(u.Field<object>("CaseID")),
        //            CaseHit = (item == null) ? 0 : TypeHelper.ParseInt(item.Field<object>("Hits_Count"))
        //        };
        //    var zx_UserCaseHit =
        //        from u in zx_UserCaseHitPre
        //        group u by u.UserID into g
        //        select new
        //        {
        //            UserID = g.Key,
        //            CaseHit = g.Sum(x => x.CaseHit)
        //        };

        //    var zx_UserProHitPre=
        //        from u in userPromotionMapDtZx.AsEnumerable()
        //        join c in promotionHitDtZx.AsEnumerable()
        //        on TypeHelper.ParseInt(u.Field<object>("ID")) equals TypeHelper.ParseInt(c.Field<object>("ObjectID")) into uc
        //        from item in uc.DefaultIfEmpty()
        //        select new
        //        {
        //            UserID = TypeHelper.ParseInt(u.Field<object>("DesignerID")),
        //            ProID = TypeHelper.ParseInt(u.Field<object>("ID")),
        //            ProHit = (item == null) ? 0 : TypeHelper.ParseInt(item.Field<object>("Hits_Count"))
        //        };
        //    var zx_UserProHit =
        //        from u in zx_UserProHitPre
        //        group u by u.UserID into g
        //        select new
        //        {
        //            UserID = g.Key,
        //            ProHit = g.Sum(x => x.ProHit)
        //        };

        //    var hit_User_Case =
        //        from u in userDtZx.AsEnumerable()
        //        join c in zx_UserCaseHit
        //        on TypeHelper.ParseInt(u.Field<object>("ID")) equals c.UserID into uc
        //        from item in uc.DefaultIfEmpty()
        //        select new
        //        {
        //            UserID = TypeHelper.ParseInt(u.Field<object>("ID")),
        //            Hit = (item == null) ? 0 : item.CaseHit
        //        };
        //    var hit_User_Case_Pro =
        //        from u in hit_User_Case
        //        join c in zx_UserProHit
        //        on u.UserID equals c.UserID into uc
        //        from item in uc.DefaultIfEmpty()
        //        select new
        //        {
        //            UserID = u.UserID,
        //            Hit = (item == null) ? u.Hit : u.Hit + item.ProHit
        //        };
        //    var hit_User_Case_Pro_Page=
        //        from u in hit_User_Case_Pro
        //        join c in pageHitDtZx.AsEnumerable()
        //        on u.UserID equals TypeHelper.ParseInt(c.Field<object>("ObjectID")) into uc
        //        from item in uc.DefaultIfEmpty()
        //        select new
        //        {
        //            UserID = u.UserID,
        //            Hit = (item == null) ? u.Hit : u.Hit + TypeHelper.ParseInt(item.Field<object>("Hits_Count")) 
        //        };
        //    //日点击量结束

        //    var query =
        //        from c in caseDtZxJc.AsEnumerable()
        //        select new
        //        {
        //            StatDate = startdate,
        //            DealerType = TypeHelper.ParseInt(c.Field<object>("WeiKe_UserID")) > 0 ? 2 : 1,
        //            DealerID = Math.Abs(TypeHelper.ParseInt(c.Field<object>("WeiKe_UserID"))),
        //            DayCaseNum = TypeHelper.ParseInt(c.Field<object>("caseNum"))
        //        };
        //    var zxCase =
        //        from c in query
        //        where c.DealerType == 2
        //        select c;
        //    var zx_User_Case =
        //        from u in userDtZx.AsEnumerable()
        //        join c in zxCase
        //        on TypeHelper.ParseInt(u.Field<object>("ID")) equals c.DealerID into uc
        //        from item in uc.DefaultIfEmpty()
        //        select new
        //        {
        //            StatDate = startdate,
        //            DealerType = 2,
        //            DealerID = TypeHelper.ParseInt(u.Field<object>("ID")),
        //            DayCaseNum = (item == null) ? 0 : item.DayCaseNum,
        //            CityName = u.Field<object>("CityName").ToString(),
        //            DealerName = u.Field<object>("DealerName").ToString(),
        //            UserName = u.Field<object>("SoufunName").ToString()
        //        };

        //    //var grouper =
        //    //    from z in zx_User_Case
        //    //    group z by new { z.StatDate, z.DealerType, z.DealerID } into g
        //    //    where g.Count() > 1
        //    //    select new
        //    //    {
        //    //        Key = g.Key,
        //    //        num = g.Count()
        //    //    };
        //    //int gcount = grouper.Count();

        //    var zx_User_Case_Pro =
        //        from z in zx_User_Case
        //        join p in promotionDtZx.AsEnumerable()
        //        on z.DealerID equals TypeHelper.ParseInt(p.Field<object>("DesignerID")) into zp
        //        from item in zp.DefaultIfEmpty()
        //        select new
        //        {
        //            StatDate = z.StatDate,
        //            DealerType = z.DealerType,
        //            DealerID = z.DealerID,
        //            DayCaseNum = z.DayCaseNum,
        //            DayProNum = (item == null) ? 0 : TypeHelper.ParseInt(item.Field<object>("proNum")),
        //            CityName = z.CityName,
        //            DealerName = z.DealerName,
        //            UserName = z.UserName
        //        };

        //    var zx_Refresh =
        //        from r in refreshDtZxJc.AsEnumerable()
        //        where TypeHelper.ParseLong(r.Field<object>("OperateUserID")) > 0
        //        select new
        //        {
        //            DealerID = TypeHelper.ParseLong(r.Field<object>("OperateUserID")),
        //            DayRefreshNum = TypeHelper.ParseInt(r.Field<object>("RefreshNum"))
        //        };

        //    var zx_User_Case_Pro_Refresh =
        //        from z in zx_User_Case_Pro
        //        join r in zx_Refresh
        //        on z.DealerID equals r.DealerID into zr
        //        from item in zr.DefaultIfEmpty()
        //        select new
        //        {
        //            StatDate = z.StatDate,
        //            DealerType = z.DealerType,
        //            DealerID = z.DealerID,
        //            DayCaseNum = z.DayCaseNum,
        //            DayProNum = z.DayProNum,
        //            DayRefreshNum = (item == null) ? 0 : item.DayRefreshNum,
        //            CityName = z.CityName,
        //            DealerName = z.DealerName,
        //            UserName = z.UserName
        //        };

        //    var zx_Reserve =
        //        from rs in reserveDtZxJc.AsEnumerable()
        //        where TypeHelper.ParseInt(rs.Field<object>("channel")) == 2
        //        select new 
        //        {
        //            UserID= TypeHelper.ParseInt( rs.Field<object>("UserID")),
        //            ReserveNum= TypeHelper.ParseInt( rs.Field<object>("ReserveNum"))
        //        };
        //    var zx_User_Case_Pro_Refresh_Reserve =
        //        from z in zx_User_Case_Pro_Refresh
        //        join rs in zx_Reserve
        //        on z.DealerID equals rs.UserID into zr
        //        from item in zr.DefaultIfEmpty()
        //        select new
        //        {
        //            StatDate = z.StatDate,
        //            DealerType = z.DealerType,
        //            DealerID = z.DealerID,
        //            DayCaseNum = z.DayCaseNum,
        //            DayProNum = z.DayProNum,
        //            DayRefreshNum = z.DayRefreshNum,
        //            DayReserveNum = (item == null) ? 0 : item.ReserveNum,
        //            CityName = z.CityName,
        //            DealerName = z.DealerName,
        //            UserName = z.UserName
        //        };

        //    //var grouper =
        //    //    from z in zx_User_Case_Pro_Refresh_Reserve
        //    //    group z by new { z.StatDate, z.DealerType, z.DealerID } into g
        //    //    where g.Count() > 1
        //    //    select new
        //    //    {
        //    //        Key = g.Key,
        //    //        num = g.Count()
        //    //    };
        //    //int gcount = grouper.Count();

        //    var zx_User_Case_Pro_Ref_Res_Tel =
        //        from z in zx_User_Case_Pro_Refresh_Reserve
        //        join t in zxTelList
        //        on z.DealerID equals t.UserID into zt
        //        from item in zt.DefaultIfEmpty()
        //        select new
        //        {
        //            StatDate = z.StatDate,
        //            DealerType = z.DealerType,
        //            DealerID = z.DealerID,
        //            DayCaseNum = z.DayCaseNum,
        //            DayProNum = z.DayProNum,
        //            DayRefreshNum = z.DayRefreshNum,
        //            DayReserveNum = z.DayReserveNum,
        //            DayTelNum = (item == null) ? 0 : item.TelNum,
        //            CityName = z.CityName,
        //            DealerName = z.DealerName,
        //            UserName = z.UserName
        //        };

        //    //var grouper =
        //    //    from z in zx_User_Case_Pro_Ref_Res_Tel
        //    //    group z by new { z.StatDate, z.DealerType, z.DealerID } into g
        //    //    where g.Count() > 1
        //    //    select new
        //    //    {
        //    //        Key = g.Key,
        //    //        num = g.Count()
        //    //    };
        //    //int gcount = grouper.Count();

        //    var zx_User_Case_Pro_Ref_Res_Tel_Prod_Price =
        //        from z in zx_User_Case_Pro_Ref_Res_Tel
        //        join p in zxProductPriceList
        //        on z.DealerID equals p.UserID into zp
        //        from item in zp.DefaultIfEmpty()
        //        select new
        //        {
        //            StatDate = z.StatDate,
        //            DealerType = z.DealerType,
        //            DealerID = z.DealerID,
        //            DayCaseNum = z.DayCaseNum,
        //            DayProNum = z.DayProNum,
        //            DayProductNum = (item == null) ? 0 : item.ProductNum,
        //            DayPriceNum = (item == null) ? 0 : item.PriceNum,
        //            DayRefreshNum = z.DayRefreshNum,
        //            DayReserveNum = z.DayReserveNum,
        //            DayTelNum = z.DayTelNum,
        //            DayHitNum = (item == null) ? 0 : item.JcHit,
        //            CityName = z.CityName,
        //            DealerName = z.DealerName,
        //            UserName = z.UserName
        //        };
        //    //var grouper =
        //    //    from z in zx_User_Case_Pro_Ref_Res_Tel_Prod_Price
        //    //    group z by new { z.StatDate, z.DealerType, z.DealerID } into g
        //    //    where g.Count() > 1
        //    //    select new
        //    //    {
        //    //        Key = g.Key,
        //    //        num = g.Count()
        //    //    };
        //    //int gcount = grouper.Count();

        //    var zx_User_Case_Pro_Ref_Res_Tel_Prod_Price_Hit =
        //        from z in zx_User_Case_Pro_Ref_Res_Tel_Prod_Price
        //        join u in hit_User_Case_Pro_Page
        //        on z.DealerID equals u.UserID into zu
        //        from item in zu.DefaultIfEmpty()
        //        select new
        //        {
        //            StatDate = z.StatDate,
        //            DealerType = z.DealerType,
        //            DealerID = z.DealerID,
        //            DayCaseNum = z.DayCaseNum,
        //            DayProNum = z.DayProNum,
        //            DayProductNum = z.DayProductNum,
        //            DayPriceNum = z.DayPriceNum,
        //            DayRefreshNum = z.DayRefreshNum,
        //            DayReserveNum = z.DayReserveNum,
        //            DayTelNum = z.DayTelNum,
        //            DayHitNum = (item == null) ? 0 : item.Hit,
        //            CityName = z.CityName,
        //            DealerName = z.DealerName,
        //            UserName = z.UserName
        //        };

        //    var result =
        //        from z in zx_User_Case_Pro_Ref_Res_Tel_Prod_Price_Hit
        //        where (z.DayCaseNum > 0 || z.DayProNum > 0 || z.DayRefreshNum > 0 || z.DayReserveNum > 0 || z.DayTelNum > 0
        //        || z.DayPriceNum > 0 || z.DayPriceNum > 0 || z.DayHitNum > 0) && z.DealerID != 0
        //        select z;

        //    //var result2 = result.Distinct(r=>);

        //    //var result =
        //    //    from z in zx_User_Case_Pro_Refresh_Reserve
        //    //    where (z.DayCaseNum > 0 || z.DayProNum > 0 || z.DayRefreshNum > 0 || z.DayReserveNum > 0 ) && z.DealerID != 0
        //    //    select z;

        //    targetDTzx = PrepareSchemaUdtStatDealer();

        //    foreach (var item in result)
        //    {
        //        DataRow row = targetDTzx.NewRow();
        //        row["StatDate"] = TypeHelper.GetDay(item.StatDate);
        //        row["DealerType"] = item.DealerType;
        //        row["DealerID"] = item.DealerID;
        //        row["DayCaseNum"] = item.DayCaseNum;
        //        row["DayProNum"] = item.DayProNum;
        //        //row["DayProductNum"] = item.DayProductNum;
        //        //row["DayPriceNum"] = item.DayPriceNum;
        //        row["DayRefreshNum"] = item.DayRefreshNum;
        //        row["DayReserveNum"] = item.DayReserveNum;
        //        //row["DayHitNum"] = item.DayHitNum;
        //        row["DayTelNum"] = item.DayTelNum;

        //        row["CityName"] = item.CityName;
        //        row["DealerName"] = item.DealerName;
        //        row["UserName"] = item.UserName;
        //        row["DayHitNumZx"] = item.DayHitNum;

        //        targetDTzx.Rows.Add(row);
        //    }

        //    targetDTzx = StatDealerWorkerJC.processZXDealer(targetDTzx, startdate);

        //    foreach (DataRow row in targetDTzx.Rows)
        //    {
        //        row["DayHitNum"] = TypeHelper.ParseInt(row["DayHitNumZx"]) + TypeHelper.ParseInt(row["DayHitNumJc"]);
        //    }
        //}
        public static DataTable PrepareSchemaUdtStatDealer()
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[]
            { 
                new DataColumn("DealerType",typeof(int)),
                new DataColumn("DealerID",typeof(int)),
                new DataColumn("StatDate",typeof(DateTime)),
                new DataColumn("DayCaseNum",typeof(int)),
                new DataColumn("DayProNum",typeof(int)),
                new DataColumn("DayProductNum",typeof(int)),
                new DataColumn("DayPriceNum",typeof(int)),
                new DataColumn("DayRefreshNum",typeof(int)),
                new DataColumn("DayReserveNum",typeof(int)),
                new DataColumn("DayHitNum",typeof(int)),
                new DataColumn("DayTelNum",typeof(int)),
                new DataColumn("CityName",typeof(string)),
                new DataColumn("DealerName",typeof(string)),
                new DataColumn("UserName",typeof(string)),
                new DataColumn("DayHitNumZx",typeof(int)),
                new DataColumn("DayHitNumJc",typeof(int))
            });
            return dt;
        }

        protected override void WriteData()
        {
            //DAL.TVPUpdate_Retry(targetDT, "StatDealerInsert05", "UdtStatDealer", "ZxbWriter");
            //DAL.TVPUpdate_Retry(targetDT, string.Concat("StatDealerInsert", startdate.ToString("MM")), "UdtStatDealer", "ZxbWriter");
            //DAL.TVPUpdate_Retry_Split(targetDTzx, string.Concat("StatDealerInsert", startdate.ToString("MM")), "UdtStatDealer", "ZxbWriter");
            //DAL.TVPUpdate_Retry_Split(targetDTjc, string.Concat("StatDealerInsert", startdate.ToString("MM")), "UdtStatDealer", "ZxbWriter");
            DAL.TVPUpdate_Retry_Split(targetDTzx, "StatDealerInsertTotal", "UdtStatDealer", "local");
            DAL.TVPUpdate_Retry_Split(targetDTjc, "StatDealerInsertTotal", "UdtStatDealer", "local");
        }
    }

    public class FakeDataWorker:BatchWorker
    {
        private DataTable splitdt;

        protected override void ReadData()
        {
            
        }

        protected override void ProcessData()
        {
            splitdt = StatDealerTotalWorker.PrepareSchemaUdtStatDealer();

            string[] cityarray = new string[3] { "北京", "上海", "广州" };
            Random rm = new Random(DateTime.Now.Millisecond);
            DateTime startDay = new DateTime(2011, 1, 1);
            int rowNum = 1000 * 1000 * 10;
            for (int i = 0; i < rowNum; i++)
            {
                DataRow row = splitdt.NewRow();
                row["DealerType"] = 2;
                row["DealerID"] = rowNum + i % 1000;
                row["StatDate"] = startDay.AddDays((-1) * i / 1000);

                row["DayCaseNum"] = rm.Next(80);
                row["DayProNum"] = rm.Next(9);
                row["DayProductNum"] = rm.Next(10);
                row["DayPriceNum"] = rm.Next(100);
                row["DayRefreshNum"] = rm.Next(90);
                row["DayReserveNum"] = rm.Next(11);
               
                row["DayTelNum"] = rm.Next(19);
                row["CityName"] = cityarray[i % 3];
                row["DealerName"] = string.Concat("dianpu", i % 1000);
                row["UserName"] = string.Concat("user", i % 1000);
                int zxHit = rm.Next(12345);
                int jcHit = rm.Next(12345);
                row["DayHitNumZx"] = zxHit;
                row["DayHitNumJc"] = jcHit;
                row["DayHitNum"] = zxHit + jcHit;

                splitdt.Rows.Add(row);
                if (splitdt.Rows.Count >= 10000 || i == rowNum - 1)
                {
                    bool good = DAL.TVPUpdate_Retry(splitdt, "StatDealerInsertTotal", "UdtStatDealer", "local");
                    if (!good)
                    {
                        Logger.InfoLog.Error(string.Format("TVPUpdate_Retry_Split出错：{0},第{1}项之前", "StatDealerInsertTotal", i));
                        break;
                    }
                    splitdt.Clear();
                }
            }
        }

        protected override void WriteData()
        {
            //DAL.TVPUpdate_Retry_Split(targetDt, "StatDealerInsertTotal", "UdtStatDealer", "local");
        }
    }
}
